package uit.bus;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class HCMBusActivity extends MapActivity {
	/** Called when the activity is first created. */

	public static MapView mapView;
	public Menu menu;
	public static SharedPreferences sharedPre;
	public String prefName = "HCMBus";
	public RadioDialog radioDialog;
	public SharedPrefren prefs;
	public MapController mc;
	public GeoPoint currentPoint;
	public GeoPoint touchedPoint;
	public MapOverlay mapOverlay;
	public MyItemizedOverlay itemziedCurrent;
	public MyItemizedOverlay itemLoTrinhdi, itemLoTrinhve, itemdanhdau;
	public LocationManager lm;
	public LocationListener locationListener;
	public boolean currentState = false;
	public DBAdapter db;
	public GeoPoint Point;
	public String[] itemsTuyen;
	public int[] listMaTuyen;
	public int matuyen;
	public CopyDB copy;
	public boolean ttNhacNho;
	public int[] bkKCNN = { 300, 500, 700 };
	public MediaPlayer song;
	public String[] itemsdulich = { "Nhà Thờ Đức Bà", "Dinh Độc Lập",
			"Chợ Bến Thành" };
	public GeoPoint gpDucBa = new GeoPoint(10780655, 106699083);
	public GeoPoint gpDinhDocLap = new GeoPoint(10777330, 106695527);
	public GeoPoint gpBenThanh = new GeoPoint(10772629, 106697931);
	public Dialog dialog;
	public Location locationEnd, locationdd;
	Drawable drawable;
	MyItemizedOverlayBall itemizedOverlayNear;

	public long start = 0;
	public long stop = 0;

	public AlertDialog.Builder buildernn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		copy = new CopyDB(this);
		copy.checkDB();

		db = new DBAdapter(this);

		mapView = (MapView) findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);
		mapView.setTraffic(true);
		mc = mapView.getController();

		Touchy t = new Touchy();
		mapView.getOverlays().add(t);

		sharedPre = getSharedPreferences(prefName, MODE_PRIVATE);
		prefs = new SharedPrefren(sharedPre);
		setModeView(prefs.getModeView());
		setStartPlace();

		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationListener = new MylocationListener();
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1,
				locationListener);

		itemziedCurrent = new MyItemizedOverlay(HCMBusActivity.this
				.getResources().getDrawable(R.drawable.my_location), this);
		itemLoTrinhdi = new MyItemizedOverlay(HCMBusActivity.this
				.getResources().getDrawable(R.drawable.tramdi), this);
		itemLoTrinhve = new MyItemizedOverlay(HCMBusActivity.this
				.getResources().getDrawable(R.drawable.tramve), this);
		itemdanhdau = new MyItemizedOverlay(HCMBusActivity.this.getResources()
				.getDrawable(R.drawable.danhdau), this);
		drawable = getResources().getDrawable(R.drawable.bus_stop);
		itemizedOverlayNear = new MyItemizedOverlayBall(drawable, mapView);

		buildernn = new AlertDialog.Builder(this);
		dialog = new Dialog(this);
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		MapController mc = mapView.getController();

		switch (keyCode) {
		case KeyEvent.KEYCODE_I:
			mc.zoomIn();
			break;

		case KeyEvent.KEYCODE_O:
			mc.zoomOut();
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		MenuItem mnu1 = menu.add(0, 1, 1, "Vị trí hiện tại");
		mnu1.setIcon(android.R.drawable.ic_menu_mylocation);

		MenuItem mnu2 = menu.add(0, 2, 2, "Tuyến xe");
		mnu2.setIcon(android.R.drawable.ic_menu_info_details);

		MenuItem mnu3 = menu.add(0, 3, 3, "Tìm kiếm");
		mnu3.setIcon(android.R.drawable.ic_menu_search);

		MenuItem mnu4 = menu.add(0, 4, 4, "Lộ trình");
		mnu4.setIcon(android.R.drawable.ic_menu_directions);

		MenuItem mnu5 = menu.add(0, 5, 5, "Chế độ xem");
		mnu5.setIcon(android.R.drawable.ic_menu_view);

		MenuItem mnu6 = menu.add(0, 6, 6, "Trợ giúp");
		mnu6.setIcon(android.R.drawable.ic_menu_help);

		MenuItem mnu7 = menu.add(0, 7, 7, "Thiết lập");
		mnu7.setIcon(android.R.drawable.ic_menu_preferences);

		MenuItem mnu8 = menu.add(0, 8, 8, "Xóa tất cả");
		mnu8.setIcon(android.R.drawable.ic_menu_delete);

		MenuItem mnu9 = menu.add(0, 9, 9, "Đến vị trí bắt đầu");
		mnu9.setIcon(android.R.drawable.ic_menu_add);

		MenuItem mnu10 = menu.add(0, 10, 10, "Địa điểm tham quan du lịch");
		mnu10.setIcon(android.R.drawable.ic_menu_help);

		MenuItem mnu11 = menu.add(0, 11, 11, "Các trạm ở gần tôi");
		mnu11.setIcon(android.R.drawable.ic_menu_help);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case 1:
			// menu vị trí hiện tại
			goCurrentPlace();
			break;
		case 2:
			// menu tuyến xe
			// Toast.makeText(getBaseContext(), "Tuyen xe", Toast.LENGTH_LONG)
			// .show();
			Intent i = new Intent("uit.bus.THONGTINTUYEN");
			startActivity(i);
			break;
		case 3:
			// menu tìm kiếm
			// Toast.makeText(getBaseContext(), "Tim kiem", Toast.LENGTH_LONG)
			// .show();
			Intent i1 = new Intent("uit.bus.TIMKIEM");
			startActivity(i1);
			break;
		case 4:
			// menu lộ trình
			// Toast.makeText(getBaseContext(), "Lộ Trình", Toast.LENGTH_LONG)
			// .show();
			lotrinhtuyen();
			break;
		case 5:
			// menu chế độ xem
			radioDialog = new RadioDialog(this, mapView, sharedPre);
			break;

		case 6:
			// menu trợ giúp
			// Toast.makeText(getBaseContext(), "Tro giup", Toast.LENGTH_LONG)
			// .show();
			trogiup();
			break;
		case 7:
			// menu thiết lập tùy chọn
			// cbDialog = new CheckboxDialog(this, mapView, sharedPre);
			Intent i6 = new Intent("uit.bus.THIETLAP");
			Bundle extras = new Bundle();
			int latCenter = mapView.getMapCenter().getLatitudeE6();
			int longCenter = mapView.getMapCenter().getLongitudeE6();
			int zoom = mapView.getZoomLevel();
			extras.putInt("latCenter", latCenter);
			extras.putInt("longCenter", longCenter);
			extras.putInt("zoom", zoom);
			i6.putExtras(extras);
			startActivity(i6);
			break;
		case 8:
			// xoa tat ca
			clearAll();
			break;
		case 9:
			// den vi tri bat dau
			gotoStartPlace();
			break;
		case 10:
			// du lich
			dulich();
			break;
		case 11:
			// các trạm ở gần
			locationEnd = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			if (locationEnd != null) {
				if (itemdanhdau.size() > 0) {
					itemdanhdau.removeAll();
				}
				tramgan(locationEnd);
			} else {
				Toast.makeText(getBaseContext(), "Chưa bật GPS...",
						Toast.LENGTH_LONG).show();
			}
			break;
		default:
			break;
		}
		return false;
	}

	public void setModeView(int mode) {
		switch (mode) {
		case 0:
			if (mapView.isSatellite()) {
				mapView.setTraffic(true);
				mapView.setSatellite(false);
			}
			break;
		case 1:
			if (mapView.isTraffic()) {
				mapView.setSatellite(true);
				mapView.setTraffic(false);
			}
			break;
		default:
			break;
		}
	}

	public void setStartPlace() {
		GeoPoint point = prefs.getStartPlace();
		int lati = 0, longi = 0, zoom = 0;
		zoom = prefs.getZoomLevel();
		lati = point.getLatitudeE6();
		longi = point.getLongitudeE6();
		if (lati != 0 && longi != 0) {
			mc.animateTo(point);
			mapView.invalidate();
		}
		if (zoom != 0) {
			mc.setZoom(zoom);
		}
	}

	public void gotoStartPlace() {
		GeoPoint point = prefs.getStartPlace();
		int lati = 0, longi = 0, zoom = 0;
		zoom = prefs.getZoomLevel();
		lati = point.getLatitudeE6();
		longi = point.getLongitudeE6();
		if (lati != 0 && longi != 0) {
			mc.animateTo(point);
			mapView.invalidate();
		} else {
			Toast.makeText(this, "Bạn chưa thiết lập vị trí bắt đầu...",
					Toast.LENGTH_LONG).show();
		}
		if (zoom != 0) {
			mc.setZoom(zoom);
		}
	}

	public class MylocationListener implements LocationListener {
		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			if (location != null) {
				currentState = true;
				ttNhacNho = prefs.getNhacNho();
				// Log.d("tragthai=", String.valueOf(ttNhacNho));
				if (ttNhacNho) {
					// tinh khoang cach
					nhacnho(location);
				}
				if (prefs.getTTDuLich1()) {
					nhacnhoDL(location, 1);
				}
				if (prefs.getTTDuLich2()) {
					nhacnhoDL(location, 2);
				}
				if (prefs.getTTDuLich3()) {
					nhacnhoDL(location, 3);
				}
				int lat = (int) (location.getLatitude() * 1E6);
				int longt = (int) (location.getLongitude() * 1E6);
				currentPoint = new GeoPoint(lat, longt);
				OverlayItem overlayItem = new OverlayItem(currentPoint, "ten",
						"tuyen so");

				if (itemziedCurrent.size() > 0) {
					itemziedCurrent.removeItem(0);
				}
				itemziedCurrent.addOverlay(overlayItem);
				mapView.getOverlays().add(itemziedCurrent);
				if (itemizedOverlayNear.size() > 0) {
					mapView.getOverlays().add(itemizedOverlayNear);
				}
				mapView.invalidate();
			}
		}

		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
		}
	}

	public void nhacnho(Location current) {
		Location point = prefs.getViTriNN();
		int kc = Math.round(current.distanceTo(point));
		// Toast.makeText(getBaseContext(),
		// "khoang cach = " + String.valueOf(kc) + "m", Toast.LENGTH_LONG)
		// .show();
		int kcnn = prefs.getKCNN();
		int ambao = prefs.getAmBao();
		if (kc <= bkKCNN[kcnn]) {
			prefs.setNhacNho(false);
			if (ambao == 0) {
				song = MediaPlayer.create(this, R.raw.ringring1);
			} else {
				if (ambao == 1) {
					song = MediaPlayer.create(this, R.raw.ringring2);
				} else {
					if (ambao == 2) {
						song = MediaPlayer.create(this, R.raw.ringring3);
					} else {
						song = MediaPlayer.create(this, R.raw.ringring4);
					}
				}
			}

			song.start();
			// song.setLooping(true);
			// AlertDialog.Builder buildernn = new Builder(HCMBusActivity.this);
			buildernn.setTitle("Nhắc nhở...");
			buildernn.setMessage("Đã đến gần..." + prefs.getTenTram());
			buildernn.setIcon(R.drawable.hurry);
			buildernn.setPositiveButton("OK", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					song.stop();
					dialog.dismiss();
				}
			});
			buildernn.show();
		}
	}

	public void nhacnhoDL(Location current, final int index) {
		Location point = new Location("diem du lich");
		if (index == 1) {
			point.setLatitude(gpDucBa.getLatitudeE6() / 1E6);
			point.setLongitude(gpDucBa.getLongitudeE6() / 1E6);
		} else {
			if (index == 2) {
				point.setLatitude(gpDinhDocLap.getLatitudeE6() / 1E6);
				point.setLongitude(gpDinhDocLap.getLongitudeE6() / 1E6);
			} else {
				point.setLatitude(gpBenThanh.getLatitudeE6() / 1E6);
				point.setLongitude(gpBenThanh.getLongitudeE6() / 1E6);
			}
		}

		int kc = Math.round(current.distanceTo(point));
		int kcnn = prefs.getKCNN();
		int ambao = prefs.getAmBao();
		if (kc <= bkKCNN[kcnn]) {

			if (ambao == 0) {
				song = MediaPlayer.create(this, R.raw.ringring1);
			} else {
				if (ambao == 1) {
					song = MediaPlayer.create(this, R.raw.ringring2);
				} else {
					if (ambao == 2) {
						song = MediaPlayer.create(this, R.raw.ringring3);
					} else {
						song = MediaPlayer.create(this, R.raw.ringring4);
					}
				}
			}

			song.start();
			// song.setLooping(true);

			// dialog = new Dialog(HCMBusActivity.this);
			dialog.setContentView(R.layout.dulich);
			TextView tvten = (TextView) dialog.findViewById(R.id.tvten);
			TextView tvdiachi = (TextView) dialog.findViewById(R.id.tvdiachi);
			ImageView image = (ImageView) dialog.findViewById(R.id.imagedl);
			Button btOK = (Button) dialog.findViewById(R.id.btOKDL);
			btOK.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					dialog.dismiss();
					song.stop();
					Intent i = new Intent("uit.bus.DLCT");
					Bundle extras = new Bundle();
					extras.putInt("key", index);
					i.putExtras(extras);
					startActivity(i);
				}
			});
			Button btClose = (Button) dialog.findViewById(R.id.btCloseDL);
			btClose.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					dialog.dismiss();
					song.stop();
				}
			});

			if (index == 1) {
				prefs.setTTDuLich1(false);
				tvten.setText("Nhà Thờ Đức Bà");
				tvdiachi.setText("Công xã Paris, Đồng Khởi, Quận 1");
				image.setImageResource(R.drawable.duc_ba);
			}
			if (index == 2) {
				prefs.setTTDuLich2(false);
				tvten.setText("Dinh Độc Lập");
				tvdiachi.setText("106 Nguyễn Du, Quận 1");
				image.setImageResource(R.drawable.thong_nhat);
			}
			if (index == 3) {
				prefs.setTTDuLich3(false);
				tvten.setText("Chợ Bến Thành");
				tvdiachi.setText("Lê Thánh Tôn, Quận 1");
				image.setImageResource(R.drawable.ben_thanh);
			}
			dialog.setTitle("Điểm du lịch...");
			dialog.show();
		}
	}

	public void goCurrentPlace() {
		if (currentState) {
			mc.animateTo(currentPoint);
		} else {
			Toast.makeText(
					getBaseContext(),
					"Không xác định được vị trí hiện tại, hãy kiểm tra lại GPS...",
					Toast.LENGTH_LONG).show();
		}
	}

	public void lotrinhtuyen() {
		getItems();
		AlertDialog.Builder lotrinh = new Builder(this);
		lotrinh.setTitle("Xem lộ trình các tuyến");
		lotrinh.setIcon(R.drawable.icon);
		lotrinh.setSingleChoiceItems(itemsTuyen, 0,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						// Toast.makeText(getBaseContext(),
						// "click " + listMaTuyen[which],
						// Toast.LENGTH_LONG).show();
						matuyen = listMaTuyen[which];
					}
				});
		lotrinh.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				// Toast.makeText(getBaseContext(), "OK" +
				// String.valueOf(which),
				// Toast.LENGTH_LONG).show();
				int luot = prefs.getLoTrinh();
				if (matuyen == 19 || matuyen == 150 || matuyen == 8) {
					if (itemLoTrinhdi.size() > 0) {
						itemLoTrinhdi.removeAll();
					}
					if (itemLoTrinhve.size() > 0) {
						itemLoTrinhve.removeAll();
					}
					if (luot == 0) {
						velotrinhdi(matuyen);
					} else {
						if (luot == 1) {
							velotrinhve(matuyen);
						} else {
							// ca di va ve
							velotrinhdi(matuyen);
							velotrinhve(matuyen);
						}
					}
				}
			}
		});
		lotrinh.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						// Toast.makeText(getBaseContext(),
						// "Cancel" + String.valueOf(which),
						// Toast.LENGTH_LONG).show();
					}
				});
		lotrinh.show();
	}

	public void dulich() {
		// getItems();

		AlertDialog.Builder dulich = new Builder(this);
		dulich.setTitle("Nhắc nhở khi đi qua...");
		dulich.setIcon(R.drawable.icon);
		boolean[] states = prefs.getListStatusDL();
		dulich.setMultiChoiceItems(itemsdulich, states,
				new OnMultiChoiceClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which,
							boolean isChecked) {
						// TODO Auto-generated method stub

						switch (which) {
						case 0:
							prefs.setTTDuLich1(isChecked);
							break;
						case 1:
							prefs.setTTDuLich2(isChecked);
							break;
						case 2:
							prefs.setTTDuLich3(isChecked);
							break;
						default:
							break;
						}
					}
				});
		dulich.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});
		dulich.show();
	}

	public void getItems() {
		int index = 0;
		db.open();
		Cursor mycurCursor = db.getListTuyen();
		int length = mycurCursor.getCount();
		itemsTuyen = new String[length];
		listMaTuyen = new int[length];
		if (mycurCursor.moveToFirst()) {
			do {
				itemsTuyen[index] = "Tuyến Số : "
						+ String.valueOf(mycurCursor.getInt(0));
				listMaTuyen[index] = mycurCursor.getInt(0);
				index++;
			} while (mycurCursor.moveToNext());
		}
		db.close();
	}

	public void velotrinhdi(int matuyen) {
		db.open();
		Cursor mycursor = db.getLoTrinh(matuyen, 0);
		if (mycursor.moveToFirst()) {
			do {
				Point = new GeoPoint((int) (mycursor.getDouble(5) * 1E6),
						(int) (mycursor.getDouble(6) * 1E6));
				OverlayItem overlayItem = new OverlayItem(Point, "Tuyến Số "
						+ String.valueOf(matuyen), "Trạm "
						+ mycursor.getString(4));
				itemLoTrinhdi.addOverlay(overlayItem);
			} while (mycursor.moveToNext());
			mapView.getOverlays().add(itemLoTrinhdi);
			mapView.invalidate();
			db.close();
		}
	}

	public void velotrinhve(int matuyen) {
		db.open();
		Cursor mycursor = db.getLoTrinh(matuyen, 1);
		if (mycursor.moveToFirst()) {
			do {
				Point = new GeoPoint((int) (mycursor.getDouble(5) * 1E6),
						(int) (mycursor.getDouble(6) * 1E6));
				OverlayItem overlayItem = new OverlayItem(Point, "Tuyến Số "
						+ String.valueOf(matuyen), "Trạm "
						+ mycursor.getString(4));
				itemLoTrinhve.addOverlay(overlayItem);
			} while (mycursor.moveToNext());
			mapView.getOverlays().add(itemLoTrinhve);
			mapView.invalidate();
		}
		db.close();
	}

	public static SharedPreferences getSharePred() {
		return sharedPre;
	}

	public void clearAll() {
		if (itemLoTrinhdi.size() > 0) {
			itemLoTrinhdi.removeAll();
		}
		if (itemLoTrinhve.size() > 0) {
			itemLoTrinhve.removeAll();
		}
		if (itemziedCurrent.size() > 0) {
			itemziedCurrent.removeAll();
		}
		if (itemizedOverlayNear.size() > 0) {
			itemizedOverlayNear.removeAll();
		}
		itemizedOverlayNear.hideBalloon();
		if (itemdanhdau.size() > 0) {
			itemdanhdau.removeAll();
		}

		mapView.invalidate();
	}

	public void trogiup() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(android.R.drawable.ic_menu_help);
		builder.setTitle("Trợ giúp");
		builder.setMessage(Html
				.fromHtml("Trường DH CNTT - Khoa Hệ thống thông tin<br><br>"
						+ "<font color='#CD7F32'>"
						+ "Hệ thống tra cứu và tìm kiếm xe Buýt TP.HCM<br><br>"
						+ "</font>"
						+ "<font color='#CD7F32'>"
						+ "GVHD  :"
						+ "</font>"
						+ " TS.Nguyễn Anh Tuấn<br>"
						+ "<font color='#CD7F32'>"
						+ "Tác giả : <br>"
						+ "</font>"
						+ "Nguyễn Đăng Thái - 07520313<br>"));

		builder.setNegativeButton("OK", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		builder.show();
	}

	public void tramgan(Location location) {
		db.open();
		if (itemizedOverlayNear.size() > 0) {
			itemizedOverlayNear.removeAll();
		}
		Cursor cursor = db.getListTram();
		int kcnn = bkKCNN[prefs.getKCNN()];
		Location locationTram;
		GeoPoint geoPoint;
		OverlayItem overlayItem;
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			locationTram = new Location("tram");
			locationTram.setLatitude(cursor.getDouble(1));
			locationTram.setLongitude(cursor.getDouble(2));
			int kc = Math.round(location.distanceTo(locationTram));
			if (kc <= kcnn) {
				geoPoint = new GeoPoint((int) (cursor.getDouble(1) * 1E6),
						(int) (cursor.getDouble(2) * 1E6));
				overlayItem = new OverlayItem(geoPoint, "Trạm dừng : "
						+ cursor.getString(0), "Tuyến qua : "
						+ cursor.getString(3));
				itemizedOverlayNear.addOverlay(overlayItem);
			}
		}
		if (itemizedOverlayNear.size() > 0) {
			mapView.getOverlays().add(itemizedOverlayNear);
		}
		mapView.invalidate();
	}

	class Touchy extends Overlay {

		@Override
		public boolean onTouchEvent(MotionEvent e, final MapView mapView) {
			// TODO Auto-generated method stub
			// super.onTouchEvent(e, mapView);
			if (e.getAction() == MotionEvent.ACTION_DOWN) {
				start = e.getEventTime();
				int x = (int) e.getX();
				int y = (int) e.getY();
				touchedPoint = mapView.getProjection().fromPixels(x, y);
			}
			if (e.getAction() == MotionEvent.ACTION_UP) {
				stop = e.getEventTime();
			}

			if ((stop - start) > 2000) {
				itemizedOverlayNear.hideBalloon();
				AlertDialog alert = new AlertDialog.Builder(HCMBusActivity.this)
						.create();
				alert.setTitle("Thông báo");
				alert.setMessage("Bạn có muốn xem thông tin các trạm gần vị trí này ?");
				if (itemdanhdau.size() > 0) {
					itemdanhdau.removeAll();
				}
				OverlayItem item = new OverlayItem(touchedPoint, "", "");
				itemdanhdau.addOverlay(item);
				mapView.getOverlays().add(itemdanhdau);
				locationdd = new Location("danh dau");
				locationdd
						.setLatitude((double) touchedPoint.getLatitudeE6() / 1E6);
				locationdd
						.setLongitude((double) touchedPoint.getLongitudeE6() / 1E6);
				alert.setButton("OK", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						tramgan(locationdd);
					}
				});

				alert.setButton2("CANCEL", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						if (itemdanhdau.size() > 0) {
							itemdanhdau.removeAll();
						}
						if (itemizedOverlayNear.size() > 0) {
							itemizedOverlayNear.removeAll();
						}
						mapView.invalidate();
					}
				});

				alert.show();
				return true;
			}
			return false;
		}
	}
}